//
//  SNProductionSearchView.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/23.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNProductionPlanningViewController.h"
#import "SNProductionSearchViewController.h"
#import "UIImageView+TapGesture.h"
#import "SNProductionSearchView.h"
#import "Masonry.h"
@interface SNProductionSearchView()
/** selected */
@property (nonatomic, assign,getter=isSelected) BOOL selected;
///** imageView */
//@property (weak, nonatomic) UIImageView * filterImageView;
/** splitLineView */
@property (weak, nonatomic) UIView * splitLineView ;
/** normalView */
@property (weak, nonatomic) UIView * insideView;
/** imageView */
@property (weak, nonatomic) UIImageView * iconImageView;


/** imageView */
@property (weak, nonatomic) UIView * textFieldImageView;
/** calendarImageView */
@property (weak, nonatomic) UIImageView * calendarImageView;
/** 年度列表 */
@property (nonatomic,strong) NSMutableArray<NSString *> * strings;
@end
@implementation SNProductionSearchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addRequest:frame];
    }
    return self;
}

- (void)addRequest:(CGRect)frame
{
    [[ZXHTTPManager sharedManager] queryplannameWithToken:@"" timestamp:@"" completed:^(NSDictionary *jsonDict, NSError *error) {
        
        logError;
        
        NSLog(@"%@",jsonDict);
        
        NSArray<NSDictionary *> * planNames =  jsonDict[@"result"];
        
        NSMutableArray<NSString *> * strings = @[].mutableCopy;
        [planNames enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSString * planName =  obj[@"planName"];
            [strings addObject:planName];
        }];
        
        self.strings = strings;
        
        [self addViews:frame];
        [self setupUI];
        
        //                              /*轻触 UITapGestureRecognizer*/
        /*****************************************************************************************/
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActived:)];
        //        tap.delegate = self;
        //        tap.numberOfTapsRequired = 2; // 点几次
        //        //	tap.numberOfTouchesRequired = 2; // 几根手指
        
        [self addGestureRecognizer:tap];
        
        //                              /*轻触 UITapGestureRecognizer*/
        /*****************************************************************************************/
    }];
}
- (void)tapActived:(UITapGestureRecognizer *)sender {
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    [self.superview addSubview:bgView];
    
    //    NSLog(@"she is beautiful girl");
    NSLog(@"%@ | %s",self.class,__func__);
    CGPoint point = [sender locationInView:sender.view];
    
    CGRect rect = [self.splitLineView.superview convertRect:self.splitLineView.frame toView:self];
    NSLog(@"point.x = %f",point.x);
    NSLogRect(rect);
    CGFloat maxX = CGRectGetMaxX(rect);
    NSLog(@"maxX = %f",maxX);
    if (point.x >= self.insideView.frame.origin.x && point.x <= maxX) {
        // NSLog(@"%@",@111111111111111111);
        self.selected = !self.selected;
        
        if (self.isSelected) {
            
            [self.calendarImageView removeFromSuperview];
            
            UIImageView * calendarImageView = ({
                UIImageView * imageView = [[UIImageView alloc] init];
                imageView.userInteractionEnabled = YES;
                UIImage * image = [UIImage imageNamed:@"01-year-select"];
                imageView.image = image;
                
                
                CGRect frame = [self convertRect:self.insideView.frame toView:self.superview];
                
                NSArray <NSString *>* strings = self.strings;//@[@"2014年度生产计划",@"2015年度生产计划",@"2016年度生产计划",@"2016年度生产计划",@"2016年度生产计划",@"2016年度生产计划"];
                
                imageView.frame = CGRectMake(4, CGRectGetMaxY(frame), CGRectGetMaxX(self.splitLineView.frame)+8+11, 40 * strings.count);
                [self.superview addSubview:imageView];
                [self addCanvasView:imageView.frame parentView:imageView strings:strings bgView:bgView];
                imageView;
            });
            self.calendarImageView = calendarImageView;
        }else{
            [self.calendarImageView removeFromSuperview];
        }
    }
}
- (void)addCanvasView:(CGRect)frame parentView:(UIView *)parentView strings:(NSArray<NSString *>*)strings bgView:(UIView *)bgView
{
    UIView * view = [[UIView alloc] init];
    
    view.backgroundColor = [UIColor clearColor];
    
    view.frame = CGRectMake(8, 8, frame.size.width - 8 -11, frame.size.height-8-12);
    
    [parentView addSubview:view];
    
    
    
    for(int i = 0; i < strings.count; i++) {
        
        UILabel * yearLabel = [[UILabel alloc] init];
        //------------------------------
        yearLabel.numberOfLines = 0;
        yearLabel.textColor = [UIColor blackColor];
        yearLabel.textAlignment = NSTextAlignmentCenter;
        yearLabel.text = strings[i];
        yearLabel.font = [UIFont systemFontOfSize:13];
        
        yearLabel.frame = CGRectMake(0, 0 + i * view.frame.size.height / strings.count, view.frame.size.width, view.frame.size.height / strings.count);
        
        @weakify_self
        yearLabel.sn_tapBlock = ^(){
            @strongify_self
            self.titleLabel.text = strings[i];
            self.selected = NO;
            [parentView removeFromSuperview];
            [bgView removeFromSuperview];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateYearNotification" object:nil userInfo:nil];
        };
        @weakify(bgView)
        bgView.sn_tapBlock = ^(){
            @strongify(bgView) //strong_tableView
            self.selected = NO;
            [parentView removeFromSuperview];
            [strong_bgView removeFromSuperview];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateYearNotification" object:nil userInfo:nil];
        };
        
        [view addSubview:yearLabel];
        
        
        if (i == strings.count-1) {
            //
        }else{
            UIView * splitLineView = [[UIView alloc] initWithFrame:CGRectMake(7, (i+1) * view.frame.size.height / strings.count , view.frame.size.width - 14, 1)];
            splitLineView.backgroundColor = HexRGB(0xf5f5f5);
            [view addSubview:splitLineView];
        }
    }
    
    
    
}
- (void)addViews:(CGRect)frame
{
    CGFloat w = frame.size.width;
    CGFloat h = frame.size.height;
    
    UIView * insideView = ({
        UIView * view = [[UIView alloc] init];
        
        view.backgroundColor = [UIColor whiteColor];
        view.frame = CGRectMake(12, 5, w - 12 - 12, h - 10);
        view.layer.cornerRadius = 10;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        
        view;
    });
    self.insideView = insideView;
    
    
    
    UIImageView * iconImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        UIImage * image = [UIImage imageNamed:@"icon_down_arrow"];
        imageView.image = image;
        
        imageView.frame = CGRectMake(8, (h - 10 - 6) * 0.5 , 11 , 6);
        [insideView addSubview:imageView];
        
        imageView;
    });
    self.iconImageView = iconImageView;
    
    
    
    UILabel * titleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 1;
        label.textColor = HexRGB(0x67B245);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"2017年度生产计划";
        label.font = [UIFont systemFontOfSize:13];
        CGSize size = [SNUtils calculateTextSize:label width:[UIScreen mainScreen].bounds.size.width];
        label.frame = CGRectMake(CGRectGetMaxX(iconImageView.frame), 0, size.width + 14, h - 10);
        [insideView addSubview:label];
        
        label;
    });
    self.titleLabel = titleLabel;
    NSLog(@"%@",titleLabel.text);
    
    UIView * splitLineView = ({
        UIView * view = [[UIView alloc] init];
        
        view.backgroundColor = HexRGB(0xE2E2E2);
        view.frame = CGRectMake(CGRectGetMaxX(titleLabel.frame), 3, 1, h - 10 - 6);
        [insideView addSubview:view];
        
        view;
    });
    self.splitLineView = splitLineView;
    //sn_note:=========  ============================ stone 🐳 ===========/
            UITextField * textField = ({
    
                UITextField * textField = [[UITextField alloc] init];
    
                // textField.delegate = self;
                textField.font = [UIFont systemFontOfSize:15];
                textField.borderStyle = UITextBorderStyleNone;//UITextBorderStyleRoundedRect;
                textField.backgroundColor = [UIColor whiteColor];
                textField.placeholder = @"输入姓名/电话号码";
                UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, h - 10)];
                {
                    UIImageView * imageView = [[UIImageView alloc] init];
                    UIImage * image = [UIImage imageNamed:@"icon_search"];
                    imageView.image = image;
    
                    imageView.frame = CGRectMake(13, (frame.size.height - 10 - 14)*0.5, 14, 14);
                    [leftView addSubview:imageView];
    
                }
                textField.leftView = leftView;
                textField.clearButtonMode = UITextFieldViewModeWhileEditing;
                textField.leftViewMode = UITextFieldViewModeUnlessEditing;
                textField.frame = CGRectMake(CGRectGetMaxX(splitLineView.frame), 0, w - 12- 12 - CGRectGetMaxX(splitLineView.frame), h-10);
                textField.returnKeyType = UIReturnKeySearch;
    
                [insideView addSubview:textField];
    
                // [self foo:textField];
    
/** 截图保存桌面 */
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    CGSize size = textField.bounds.size;
//                    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
//    
//                    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//                    [textField.layer renderInContext:context];
//    
//                    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//    
//                    //	UIImageWriteToSavedPhotosAlbum(newImage, NULL, NULL, NULL);
//    
//                    NSData * data = UIImagePNGRepresentation(newImage);
//                    [data writeToFile:@"/Users/stone/Desktop/nnn.png" atomically:YES];
//    
//                    UIGraphicsEndImageContext();
//                });
    
                textField;
            });
            self.textField = textField;
    //sn_note:=========  ============================ stone 🐳 ===========/
    UIView * textFieldImageView = ({
        UIView * view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
//        if (IS_IPHONE_4) {
//            
//            UIImage * image = [UIImage imageNamed:@"mmm"];
//            imageView.image = image;
//        }else{
//            
//            UIImage * image = [UIImage imageNamed:@"nnn"];
//            imageView.image = image;
//        }
        
        view.frame = CGRectMake(CGRectGetMaxX(splitLineView.frame), 0, w - 12- 12 - CGRectGetMaxX(splitLineView.frame), h-10);
        
        [insideView addSubview:view];
        
        
        view.sn_tapBlock = ^(){
            
            SNProductionSearchViewController * vc = [SNProductionSearchViewController new];
            vc.fullName = [SNProductionPlanningViewController getFullName];
            [self.nav pushViewController:vc animated:NO];
        };
        
        view;
    });
    self.textFieldImageView = textFieldImageView;
    //sn_note:=========  ============================ stone 🐳 ===========/
//    UIImageView * textFieldImageView = ({
//        UIImageView * imageView = [[UIImageView alloc] init];
//        
//        if (IS_IPHONE_4) {
//            
//            UIImage * image = [UIImage imageNamed:@"mmm"];
//            imageView.image = image;
//        }else{
//            
//            UIImage * image = [UIImage imageNamed:@"nnn"];
//            imageView.image = image;
//        }
//        
//        imageView.frame = CGRectMake(CGRectGetMaxX(splitLineView.frame), 0, w - 12- 12 - CGRectGetMaxX(splitLineView.frame), h-10);
//        
//        [insideView addSubview:imageView];
//        
//        
//        imageView.sn_tapBlock = ^(){
//            
//            SNProductionSearchViewController * vc = [SNProductionSearchViewController new];
//            [self.nav pushViewController:vc animated:NO];
//        };
//        
//        imageView;
//    });
//    self.textFieldImageView = textFieldImageView;
    //sn_note:=========  ============================ stone 🐳 ===========/
    
    
//    UIImageView * filterImageView = ({
//        UIImageView * imageView = [[UIImageView alloc] init];
//        UIImage * image = [UIImage imageNamed:@"01-filter-class"];
//        imageView.image = image;
//        imageView.frame = CGRectMake(CGRectGetMaxX(insideView.frame)+20, (h-20)*0.5, 20, 20);
//        [self addSubview:imageView];
//        imageView.sn_tapBlock = ^(){
//            
//            !self.siftBlock ?: self.siftBlock();
//        };
//        imageView;
//    });
//    self.filterImageView = filterImageView;
}
- (void)setupUI
{
    
}
@end
